package com.song.mall.order.listener;

import com.rabbitmq.client.Channel;
import com.song.common.to.mq.StockLockedTo;
import com.song.mall.order.entity.OrderEntity;
import com.song.mall.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2021/10/13 - the current system date.
 * 20:12 - the current system time.
 */
@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void handleStockRelease(OrderEntity entity, Message message, Channel channel) throws IOException {
        System.out.println("收到过期的订单 准备关闭订单"+entity.getStatus());
        try {
            orderService.closeOrder(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
           channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
